
package keul.planner.beans;

import java.awt.image.BufferedImage;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.io.Serializable;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.inject.Inject;
import keul.planner.entities.Line;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.imageio.ImageIO;
import keul.planner.entities.Station;
import keul.planner.tools.Tools;

/**
 * Die Bean für die viewLine.xhtml-Seite und einen Teil
 * der Startseite (home.xhtml). Sollte der Setter von der 
 * xhtml-Seite aus bedient werden, wird gleich die Line-Variable
 * mit dem richtigen Objekt besetzt.
 * @author kev
 */
@ManagedBean
@SessionScoped
public class LineViewManager implements Serializable {
    
    @Inject
    private DataManager dataManager;
    @Inject 
    private SessionManager sessionManager;
    
    private Line selectedLine;  
    private String lineString;
    
    public String getLineString() {
        return lineString;
    }
    /**
     * Diese Setter sucht nach dem Setzen des Strings gleich
     * das Objekt aus der Datenbank und legt die Referenz auf
     * selectedLine. Dazu wird ein Bild des Steckenverlaufs 
     * generiert.
     * @param lineString 
     */
    public void setLineString(String lineString) {
        this.lineString = lineString;
        selectedLine = dataManager.getLineByName(lineString);
        
        OutputStream os;
        BufferedImage image;

        try {
            os = new FileOutputStream("c:\\maps\\"+sessionManager.getHash()+".jpg");
            image = Tools.drawMap(selectedLine.getStops());
            ImageIO.write(image, "jpeg", os);
            os.close();
        } catch (Exception ex) {
            Logger.getLogger(RouteCalculationManager.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    public Line getSelectedLine() {
        return selectedLine;
    }
    public void setSelectedLine(Line selectedLine) {
        this.selectedLine = selectedLine;
    }
    
    /**
     * Diese Methode leitet den Aufruf an eine Methode des übergebenen
     * line-Objekts weiter.
     * @see #Line.calculateDistanceToStartStation(Station)
     * @param line
     * @param currentStationString
     * @return 
     */
    public String calculateDistanceToStartStationByString(Line line, String currentStationString) {
        double distance = 0;
        int currentIndex = line.getStops().size()-1;
        while(! line.getStops().get(currentIndex).getName().equals(currentStationString) && currentIndex>=0) {
            currentIndex--;
        }
        while (currentIndex>0) {
            distance += Station.computeGeoDistance( line.getStops().get(currentIndex),  line.getStops().get(currentIndex-1));
            currentIndex--;
        }
        if (distance==0)
            return "";
        return String.format("%.1f km", distance);
    }
    
    /**
     * Diese Methode leitet den Aufruf an eine Methode des übergebenen
     * line-Objekts weiter.
     * @see #Line.calculateDistanceToPreviousStation(Station)
     * @param line
     * @param currentStationString
     * @return 
     */    
    public String calculateDistanceToPreviousStationByString(Line line, String currentStationString) {
        double distance = 0;
        int currentIndex =  line.getStops().size()-1;
        while(! line.getStops().get(currentIndex).getName().equals(currentStationString) && currentIndex>=0) {
            currentIndex--;
        }
        if (currentIndex>0) {
            distance = Station.computeGeoDistance( line.getStops().get(currentIndex),  line.getStops().get(currentIndex-1));
        }
        if (distance==0)
            return "";
        return String.format("%.1f km", distance); 
    }        
    /**
     * Anzeige der Minuten, die nötig waren um von der vorigen zur angebenen
     * Station zu kommen.
     * @see #Line.calculateMinutesToPreviousStation(Station)
     * @param line
     * @param currentStationString
     * @return 
     */
    public String calculateMinutesToPreviousStation(Line line, String currentStationString) {
        int currentIndex = line.getStops().size()-1;
        while(!line.getStops().get(currentIndex).getName().equals(currentStationString) && currentIndex>=0) {
            currentIndex--;
        }
        double distance = line.calculateDistanceToPreviousStation(line.getStops().get(currentIndex));
        if (distance==0)
            return "";
        return (int)(distance/line.getAverageVelocity()*60) + " Min.";           
    }
    
    public String returnLinkToLineView(String lineString) {
        setLineString(lineString);
        return "viewLine";
    }
    
    public String isLineServing(Station station, String lineString) {
        if (station.isStationServedBy(dataManager.getLineByName(lineString)))
           return lineString;
        return null;
    }
        
}
